const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const WebpackMd5Hash = require("webpack-md5-hash");
const CopyWebpackPlugin = require("copy-webpack-plugin");

const devMode = process.env.NODE_ENV !== "production";

module.exports = {
  entry: {
    index: path.resolve(__dirname, "../src/index.js"),
    /* success: path.resolve(__dirname, "../src/success.js"), */
  },
  output: {
    path: path.resolve(__dirname, "../dist"),
    filename: devMode ? "[name].js" : "static/js/[name].[chunkhash].js",
  },
  module: {
    rules: [
      {
        test: /.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
          },
        },
      },
      {
        test: /\.(sa|sc|c)ss$/,
        exclude: /node_modules/,
        use: [
          "style-loader",
          MiniCssExtractPlugin.loader,
          "css-loader",
          "postcss-loader",
          "sass-loader",
        ],
      },
      {
        test: /\.(png|jpg|gif)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "url-loader", //根据文件地址加载文件
            options: {
              limit: 2048,
              name: "[name].[ext]",
              outputPath: "static/images",
              publicPath: "../images",
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: "static/css/[name].[contenthash].css",
    }),
    new HtmlWebpackPlugin({
      inject: true,
      hash: true,
      template: path.resolve(__dirname, "../public/index.html"),
      filename: "index.html",
      chunks: ["index"],
    }),
    /* new HtmlWebpackPlugin({
      inject: true,
      hash: true,
      template: path.resolve(__dirname, "../public/success.html"),
      filename: "success.html",
      chunks: ["success"],
    }), */
    /* new CopyWebpackPlugin([
      {
        from: "src/assets",
        to: "assets",
      },
    ]), */
    new WebpackMd5Hash(),
  ],
  optimization: {
    splitChunks: {
      cacheGroups: {
        styles: {
          name: "styles",
          test: /\.css$/,
          chunks: "all",
          enforce: true,
        },
      },
    },
  },
};
